package com.foreknow.demo11;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 预处理PreparedStatement接口：可以执行动态的SQL语句
 * con 表示一个活动连接：
 *
 *    PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
 *                                      SET SALARY = ? WHERE ID = ?");
 *    pstmt.setBigDecimal(1, 153833.00)
 *    pstmt.setInt(2, 110592)
 */
public class PreparedStatementDemo {
    public static void main(String[] args) {
        try {
            //1. 加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获取连接(Connection)
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Fender?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC","root","1234");
            //3.获取预处理对象
            PreparedStatement pstmt =  conn.prepareStatement("insert into admin(username,password) value (?,?)");
            //将?替换成具体的值
            pstmt.setString(1,"ewew");
            pstmt.setString(2,"22wq");
            //执行添加操作
            int isRight = pstmt.executeUpdate();
            System.out.println(isRight);
            //关闭
            pstmt.close();
            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
